package com.wo2record.user.service.impl;

import com.wo2record.base.enumerror.ErrorType;
import com.wo2record.base.exception.ValidateException;
import com.wo2record.base.service.impl.BaseServiceAbstractImpl;
import com.wo2record.base.util.ModelUtil;
import com.wo2record.user.model.UserLogin;
import com.wo2record.user.service.UserLoginService;

public class UserLoginServiceImpl extends BaseServiceAbstractImpl<UserLogin> implements UserLoginService {

	public Class<?> getObjectClass(){
		return UserLogin.class;
	}
	
	@Override
	public UserLogin registNewUser(UserLogin userLogin) throws Exception{
		saveObject(userLogin);
		return userLogin;
	}
	
	@Override
	public void validateObjectBeforeSave(UserLogin userLogin) throws Exception{
//		validatePassword(userLogin); //TODO for testing, we just comment this validation.
	}
	
	private void validatePassword(UserLogin userLogin) throws Exception{
		String password = userLogin.getLoginPassword();
		String passwordConfirm = userLogin.getLoginPasswordConfirm();
		
		if(ModelUtil.isBlank(password)){
			throw new ValidateException(ErrorType.MISSING_REQUIRED_VALUE, "Password should not be null.");
		}else if(!password.equals(passwordConfirm)){
			throw new ValidateException(ErrorType.INVALID_VALUE, "The password that you entered two times are not matched, please correct it.");
		}
	}
}
